package pacote.doandroid;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView;


/**
 * 
 * ANOTAÇÕES:
 * 
 * O LISTAR ESTÁ PRATICAMENTE PRONTO, BASTA COLOCAR A URL CORRETA DO WEB SERVICE
 * FALTA SABER COMO IRÁ MANDAR O ARQUIVO PELA URL NO SALVAR
 *
 */
		


public class AndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView textView = (TextView) findViewById(R.id.textView1);
        textView.setHeight(10);
        listeningButtonExecuteBackup();
        listeningButtonList();
    }

    //NÃO TA TERMINADO
	private void listeningButtonExecuteBackup() {
		
		Button buttonBackup = (Button) findViewById(R.id.buttonbackup);
        buttonBackup.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {
				ScrollView s = (ScrollView) findViewById(R.id.scroller);
				s.setVisibility(4);
				TextView textView = (TextView) findViewById(R.id.textView1);
				EditText editText = (EditText) findViewById(R.id.editText1);
				String cont = "";
				String filename = "";
				
				if (editText.getText().length() == 0) {
					textView.setText("Please fill the file name field.");
					return;
				}else{
					filename = editText.getText().toString();
				}
				
				File fileRaiz = Environment.getExternalStorageDirectory();
				File file = new File(fileRaiz, editText.getText().toString());
				editText.setText("");

				if (!file.canRead()) {
					cont = "File not found.";
					textView.setText(cont);
					return;
				}else{
					cont = "The file was found.";
					textView.setText(cont);
				}
				

				try {
					//TODO TEM que ver oque caracteriza o sucesso no retorno do ClientResponse.
					String cr = requestAdress("50670-380");
					String cont2  = "File \""+ filename + "\" saved successfully.";
					textView.setText(cr);	
				} catch (FileNotFoundException e) {
					String cont2  = "File \""+ filename + "\" not found.";
					textView.setText(cont + cont2);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
        });
	}
	
	  public static String requestAdress(String cep) throws Exception {
	        String xmlAdress = "";
	        URL seviceAdress = new URL(
	                "http://webservice.uni5.net/web_cep.php?auth=43e8720b77c0225e9b7ffa212c955e97&formato=xml&cep="
	                        + cep);
	        URLConnection sa = seviceAdress.openConnection();
	        BufferedReader in = new BufferedReader(new InputStreamReader(
	                sa.getInputStream()));
	        String inputLine;
	        while ((inputLine = in.readLine()) != null) {
	            xmlAdress = xmlAdress + inputLine + "\n";
	        }
	        return xmlAdress;
	    }
	  
	
	
	private void listeningButtonList() {
//		final Activity a = this;
		Button buttonList = (Button) findViewById(R.id.buttonList);
        buttonList.setOnClickListener(new Button.OnClickListener(){
			public void onClick(View v) {

				ScrollView s = (ScrollView) findViewById(R.id.scroller);
				s.setVisibility(0);
				TextView textView = (TextView) findViewById(R.id.textView1);
				textView.setText("Files....");
					TextView t1 =  (TextView) findViewById(R.id.textView5);
					try {
						//Quando o web service valendo mudar a linha abaixo
						t1.setText(requestAdress("50670-380"));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			}
        });
	}
	
}